package com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkMoreSpecific

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import com.lancoo.znbkxx.teacher.R
import com.lancoo.znbkxx.teacher.aibkGenerateTestPaper.aibkGeneratePaper.itemtouchhelper.ItemSpecificEntity
import com.lancoo.znbkxx.teacher.aibkTestCoach.aibkTools.MyToast
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerAdapter
import com.lancoo.znbkxx.teacher.aibkTestCoach.kBase.KBaseRecyclerViewHolder
import com.lancoo.znbkxx.teacher.aibkTestCoach.networkRequest.entity.TempletGrammarSpecificListInfor
import kotlinx.android.synthetic.main.aibk_adapter_more_specific_parent.view.*

class AibkMoreSpecificGrammarParentAdapter(
        val activity: AppCompatActivity,
        val templetGrammarSpecificListInfor: TempletGrammarSpecificListInfor
) : KBaseRecyclerAdapter() {

    override fun getHolderView(parent: ViewGroup?, position: Int): View? {
        return LayoutInflater.from(activity)
                .inflate(R.layout.aibk_adapter_more_specific_parent, parent, false)
    }

    override fun onBindViewHolder(holder: KBaseRecyclerViewHolder, position: Int) {
        val topicName = templetGrammarSpecificListInfor[position].TopicName

        //加载某个语法分类
        holder.convertView.TvTopicGrammarName.text = topicName
        holder.convertView.RvSpecificParent.recycledViewPool.clear()
        val adapterChild = AibkMoreSpecificGrammarChildAdapter(activity, templetGrammarSpecificListInfor, position)
        adapterChild.setClickListener(object : AibkMoreSpecificGrammarChildAdapter.OnClickListener {
            override fun onClickAllCheck(isAllSelected: Boolean) {
                //监听子类分类全选状态变化
                holder.convertView.TvCheckAllSpecific.isSelected = isAllSelected
            }

            override fun onClickSingleAnalysis(indexA: Int, indexB: Int) {
//                val dialogSpecificAnalysis = DialogSpecificAnalysis(activity, activity, templetGrammarSpecificListInfor, indexA, position, false)
//                dialogSpecificAnalysis.show()
                clickListener!!.onClickSingleAnalysis(indexA, position)
            }
        })
        holder.convertView.RvSpecificParent.adapter = adapterChild

        //显示分类全选按钮状态
        holder.convertView.TvCheckAllSpecific.isSelected = adapterChild.getIsAllSelected(position)

        //执行点击全选按钮
        holder.convertView.TvCheckAllSpecific.setOnClickListener {
            holder.convertView.TvCheckAllSpecific.isSelected = !holder.convertView.TvCheckAllSpecific.isSelected
            adapterChild.checkAllParent(position, holder.convertView.TvCheckAllSpecific.isSelected)
        }
        //点击查看某个语法分类的总体分析
        holder.convertView.TvCheckAnalysis.setOnClickListener {
//            MyToast.showToast("点击查看某个语法分类的总体分析")
//            val dialogSpecificAnalysis = DialogSpecificAnalysis(activity, activity, templetGrammarSpecificListInfor, position, 0, true)
//            dialogSpecificAnalysis.show()
            clickListener!!.onClickAllAnalysis(position, 0)
        }
    }

    override fun getItemCount(): Int {
        return templetGrammarSpecificListInfor.size
    }

    interface OnClickListener {
        fun onClickSingleAnalysis(indexA:Int,indexB:Int)
        fun onClickAllAnalysis(indexA:Int,indexB:Int)
    }

    private var clickListener: OnClickListener? = null

    fun setClickParentListener(clickListener: OnClickListener) {
        this.clickListener = clickListener
    }

}